﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using MATtutsLab.Entities;

namespace MATtutsLabs.ZombieMovieCatalog
{
    public partial class ZombieMovieCatalogView : Form
    {
        private readonly ILogger Logger = new Logger();

        private bool DisableEvents { get; set; }

        public ZombieMovieCatalogView()
        {
            InitializeComponent();

            SetUpDataGridView();
        }

        private void SetUpDataGridView()
        {
            dgMovies.ConfigureFor<Movie>()
                .AddTextColumn(e => e.Title, o => o.WithHeader("Title").WithWidth(140.0f))
                .AddTextColumn(e => e.ShortDescription, o => o.WithHeader("Synopsis"))
                .NotifyOfSelectionChange(dgMovies_SelectionChanged)
                .NotifyOfDoubleClick(dgMovies_DoubleClick);
        }

        private void dgMovies_DoubleClick()
        {
            try
            {
                //if (!DisableEvents)
            }
            catch (Exception ex)
            {
                Logger.WriteToLog(ex.Message);
            }
        }

        private void dgMovies_SelectionChanged(IList<Movie> selectedMovie)
        {
            try
            {
                //if (!DisableEvents)
            }
            catch (Exception ex)
            {
                Logger.WriteToLog(ex.Message);
            }
        }

        private void btnAddMovie_Click(object sender, EventArgs e)
        {
            try
            {
                //if (!DisableEvents)
            }
            catch (Exception ex)
            {
                Logger.WriteToLog(ex.Message);
            }
        }

        private void btnEditMovie_Click(object sender, EventArgs e)
        {
            try
            {
                //if (!DisableEvents)
            }
            catch (Exception ex)
            {
                Logger.WriteToLog(ex.Message);
            }
        }

        private void btnDeleteMovie_Click(object sender, EventArgs e)
        {
            try
            {
                //if (!DisableEvents)
            }
            catch (Exception ex)
            {
                Logger.WriteToLog(ex.Message);
            }
        }


        public void ShowMovieCoverImage(Movie movie)
        {
            pbMovieImage.ImageLocation = movie.Image;
        }

        public void DisplayMovies(IList<Movie> movies)
        {
            DisableEvents = true;

            dgMovies.DataSource = movies;

            DisableEvents = false;
        }

        public void SelectFirstMovieInList()
        {
            dgMovies.SelectFirst();
        }

        public void RefreshMovieList(IList<Movie> movies, Movie currentMovie)
        {
            InvokeAction(() => dgMovies.DataSource = movies);

            SelectMovieInList(currentMovie);
        }

        public bool PromptUserForDeleteConfirmation()
        {
            var result = MessageBox.Show("Do you really want to delete this?", "Please Confirm", MessageBoxButtons.YesNo);

            return result == DialogResult.Yes;
        }

        private void SelectMovieInList(Movie movie)
        {
            InvokeAction(() => dgMovies.SelectWhere<Movie>(m => m.Title == movie.Title));
        }

        protected void InvokeAction(Action action)
        {
            if (InvokeRequired)
                Invoke(action);
            else
                action();
        }
    }
}
